package com.hancai.barrage.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author 涵菜
 * @version 1.0
 * json:对象与字符串格式转换
 * 响应给客户端的信息
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class JsonResult<T> {
    public static final Integer DEFAULT_SUCCESS_CODE=200;
    public static final Integer DEFAULT_FAILED_CODE=500;
    public static final String DEFAULT_SUCCESS_MESSAGE="请求成功";
    public static final String DEFAULT_FAILED_MESSAGE="请求资源找到了，但内部服务器发送错误";
    private Integer code;
    private String msg;
    private T data;
    public static <T> JsonResult <T> success (String msg,T data){
        return new JsonResult<>(DEFAULT_SUCCESS_CODE,msg,data);
    }
    public static <T> JsonResult <T> success (T data){
        return success(DEFAULT_SUCCESS_MESSAGE,data);
    }
    //只响应成功，不提供消息
    public static <T> JsonResult<T> success(){
        return success(null);
    }
    public static  <T>JsonResult<T> failed(Integer code,String msg){
        return new JsonResult<>(code,msg,null);
    }
    public static <T>JsonResult<T> failed(String msg){
        return failed(DEFAULT_FAILED_CODE,msg);
    }
    //只响应失败，不提供消息
    public static <T>JsonResult<T> failed(){
        return failed(DEFAULT_FAILED_MESSAGE);
    }
}
